<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Widgets for displaying the news stream.
 * Includes a list widget and its specific item widget.
 * 
 * The Parent Classes of the _NewsStream and _NewsStreamItem.
 * Ohters SHOULD be inherited from it.
 * 
 * Or directly show all of the news!
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds the 'optionswitch' variable.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_widget.php';

// Definitions.
// After it, no item would display; Might be LARGER than the 'XXX_PERPAGE'
define('NEWSSTREAM_MAX_ITEM_COUNT',		100);


/**
 * Widget for displaying news stream.
 */
class NewsStream extends Widget
{
	/**
	 * Base news that constructed the list content.
	 */
	var $subnews = null;
	
	/**
	 * Constructor.
	 */
	function __construct($subnews, $out = null)
	{
		parent::__construct($out);
		$this->subnews = $subnews;
	}
	
	/**
	 * Starts a news stream.
	 * The notice or some sayings of the stream. Might some labels.
	 * Like 'Note new.'!
	 * 
	 * Might be overrided.!
	 */
	function showStart()
	{
		$this->out->element('div');
		$this->out->element('h3', null, '新鲜事');
		$this->out->elementStart('ol', array('class' => 'cls_ol_list'));
		
		$this->out->element('hr');
	}
	
	/**
	 * Ends a news stream.
	 */
	function showEnd()
	{
		$this->out->elementEnd('ol');
	}
	
	/**
	 * Show the news stream.
	 * 
	 * @return int, count of news listed.
	 */
	function show()
	{
		$this->showStart();
		
		$itemcnt = 0;
		while ($this->subnews->fetch() && $itemcnt <= NEWSSTREAM_MAX_ITEM_COUNT) {
			++$itemcnt;
			if ($itemcnt > NEWSSTREAM_MAX_ITEM_COUNT) {
				break;
			}
			
			try {
				$item = $this->newListItem($this->subnews);
				$item->show();
			} catch (Exception $e) {
				// FIXME: Log?
				continue;
			}
		}
		
		$this->showEnd();
		
		return $itemcnt;
	}
	
	/**
	 * Returns a new stream item for the current sub news.
	 */
	function newListItem(&$subnews)
	{
		return new NewsStreamItem($subnews, $this->out);
	}
}

/**
 * Widget for displaying a single news item with some options.
 */
class NewsStreamItem extends Widget
{
	/**
	 * The sub news this item will show().
	 */
	var $subnews = null;
	
	/**
	 * Options switchs.
	 */
	var $optionswitch = false;
	
	/**
	 * Constructor.
	 */
	function __construct($subnews, $out = null)
	{
		parent::__construct($out);
		
		$this->subnews =& $subnews;
		$this->optionswitch = Rights::hasRight(slh_get_userrights(), RIGHT_NEWS);
	}
	
	/**
	 * Recipe function for displaying a single item.
	 * 
	 * This uses all the other methods to correctly display an item. Override
	 * it or one of the others to fine-tune the output.
	 */
	function show()
	{
		if (empty($this->subnews)) {
			return;
		}
		
		$this->prepareData();
		$this->showStart();
		$this->showDataInfo();
		if (true == $this->optionswitch) {
			$this->showOptions();
		}
		$this->showEnd();
	}
	
	/**
	 * Do some data preparation.
	 */
	function prepareData()
	{
	}
	
	/**
	 * Start a single news item.
	 */
	function showStart()
	{
		$this->out->elementStart('dd', array('class' => 'cls_dd_newsitem'));
		
		$this->out->elementStart('fieldset');
		$legend = 'news id: ' . $this->subnews->news_id . 
				  '; type: ' . $this->subnews->getBaseNews()->getNewsTypeName() .
				  '; status: ' . $this->subnews->getBaseNews()->status;
		$this->out->element('legend', null, $legend);
	}
	
	/**
	 * Finish the note news item.
	 */
	function showEnd()
	{
		$this->out->elementEnd('fieldset');
		$this->out->elementEnd('dd');
	}
	
	/**
	 * Overriding. Example:
	 */
	function showDataInfo()
	{
	}
	
	/**
	 * Overriding. Example:
	 */
	function showOptions()
	{
	}
}

?>